/* 文档说明： */
/*  主要功能：封装TCP套接字的接口     */
/*   实现类能完成Socket API接口的使用 */
#pragma once

#include <iostream>
#include <string>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "Log.hpp"
extern Log lg;

enum
{
    SockErr = 1,
    BindErr,
    ListenErr,
};

class TcpSocket
{
public:
    TcpSocket()
    {
    }
    ~TcpSocket()
    {   
        Close();
    }

    void Socket() // 创建套接字的接口
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (_sockfd < 0)
        {
            lg(Fatal, "socket fail..., error: %d, reason:%s", errno, strerror(errno)); // 服务端的日志功能
            exit(SockErr);
        }
    }
    void Bind(uint16_t port) // 服务端 绑定的接口
    {
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local)); // 初始化
        socklen_t len = sizeof(local);
        // 填写字段：
        local.sin_family = AF_INET;
        local.sin_port = htons(port);
        local.sin_addr.s_addr = INADDR_ANY; // 接受任何ip

        if (bind(_sockfd, (const struct sockaddr *)&local, len) < 0)
        {
            lg(Fatal, "bind fail..., error: %d, reason:%s", errno, strerror(errno)); // 服务端的日志功能
            exit(BindErr);
        }
    }
    void Listen() // 服务端 监听的接口
    {
        int backlog = 10; //???
        if (listen(_sockfd, backlog) < 0)
        {
            lg(Fatal, "listen fail..., error: %d, reason:%s", errno, strerror(errno)); // 服务端的日志功能
            exit(ListenErr);
        }
    }
    int Accept(std::string *clientip, uint16_t *clilentport) //服务端 连接的接口\
    函数返回值返回新的fd， 函数的参数中可以带出client的ip和port！
    {
        // 构建客服端的struct sockaddr_in
        struct sockaddr_in client;
        socklen_t len;
        memset(&client, 0, sizeof(client));

        int newfd = accept(_sockfd, (struct sockaddr *)&client, &len); // 接受
        if (newfd < 0)
        {
            lg(Warning, "accept fail..., error: %d, reason:%s", errno, strerror(errno));
            return -1; // 返回-1则出现错误
        }
        // inet_ntop：
        char dst[32]; // 缓冲区
        inet_ntop(AF_INET, &client.sin_addr, dst, sizeof(dst));
        //std::cout << "#" << dst << " " << ntohs(client.sin_port) << std::endl;
        *clientip = dst;
        *clilentport = ntohs(client.sin_port);
        return newfd;
    }
    bool Connet(const std::string &ip, uint16_t port) // 客服端 连接的接口
    {
        struct sockaddr_in serve;
        socklen_t len = sizeof(serve);
        serve.sin_family = AF_INET;
        serve.sin_port = htons(port);
        inet_pton(AF_INET, ip.c_str(), &(serve.sin_addr));

        int ret_connect = connect(_sockfd, (struct sockaddr*)&serve, len);
        if(ret_connect == -1) 
        {
            std::cerr << "connect to " << ip << ":" << port << " error" << std::endl;
            return false;
        }
        return true;
    }
    void Close()
    {
        if(_sockfd >= 0)
        close(_sockfd);
    }
    int Getfd() // 客服端的接口需要得到sockfd
    {
        return _sockfd;
    }

private:
    int _sockfd;
};